<template>
  <div class="carriage">
    <van-nav-bar title="修改订单"
                 @click-left="onClickLeft"
                 @click-right="onClickRight"
                 fixed>
      <van-icon name="arrow-left"
                slot="left"
                color="#333"
                size="0.6rem" />
      <van-icon :name="require('@/assets/img/computer.png')"
                slot="right"
                color="#333"
                size="0.6rem" />
    </van-nav-bar>
    <div class="carriageInfo">
      <van-cell-group>
        <van-cell title="干线承运商"
                  :is-link="!disabledinfo"
                  :value="Carrier"
                  size="large"
                  @click="changeCarrier" />
        <van-action-sheet v-model="showCarrier"
                          :actions="actionsCarrier"
                          @select="onSelectCarrier"
                          cancel-text="取消"
                          @cancel="onCancelCarrier" />
        <template v-if="Carrier!='精拼' && (data.pushPlatform==0 || data.pushPlatform==3)">
          <van-cell title="直客承运商"
                    :is-link="!disabledinfo"
                    :value="data.kaCarrierCompanyName"
                    size="large"
                    @click="changeCarrierUser"
                    required />
          <van-field v-model="data.kaCarrierContactName"
                     type="text"
                     label="承运商联系人"
                     size="large"
                     input-align="right"
                     required
                     placeholder="请输入"
                     :disabled="disabledinfo" />
          <van-field v-model="data.kaCarrierContactMobile"
                     type="tel"
                     label="联系人手机号"
                     size="large"
                     input-align="right"
                     required
                     placeholder="请输入"
                     :disabled="disabledinfo" />
          <van-field v-model="data.carrierNumber"
                     type="number"
                     label="承运商订单号"
                     size="large"
                     input-align="right"
                     required
                     placeholder="请输入"
                     :disabled="disabledinfo" />
        </template>
        <template v-if="isdown || isdown==0">
          <van-field v-model="data.carrierPrice"
                     type="number"
                     label="干线运输应付"
                     size="large"
                     input-align="right"
                     placeholder="请输入"
                     :disabled="disabledinfo" />
          <van-cell title="提货承运商"
                    :is-link="!disabledinfo"
                    :value="pickvalue"
                    size="large"
                    @click="changePick" />
          <template v-if="data.kaIspickUp==1">
            <van-field v-model="data.carrierPickUp"
                       type="number"
                       label="提货费"
                       size="large"
                       input-align="right"
                       placeholder="请输入"
                       :disabled="disabledinfo" />
          </template>
          <van-cell title="送货承运商"
                    :is-link="!disabledinfo"
                    :value="sendvalue"
                    size="large"
                    @click="changeSend" />
          <template v-if="data.kaIssend==1">
            <van-field v-model="data.carrierSend"
                       type="number"
                       label="送货费"
                       size="large"
                       input-align="right"
                       placeholder="请输入"
                       :disabled="disabledinfo" />
          </template>
        </template>
        <template v-if="isdown===null">
          <van-field v-model="data.downCarrierPrice"
                     type="number"
                     label="干线运输应付"
                     size="large"
                     input-align="right"
                     placeholder="请输入"
                     :disabled="disabledinfo" />
          <van-cell title="提货承运商"
                    :is-link="!disabledinfo"
                    :value="pickvalue"
                    size="large"
                    @click="changePick" />
          <template v-if="data.kaIspickUp==1">
            <van-field v-model="data.downCarrierPickUp"
                       type="number"
                       label="提货费"
                       size="large"
                       maxlength="6"
                       input-align="right"
                       placeholder="请输入"
                       :disabled="disabledinfo" />
          </template>
          <van-cell title="送货承运商"
                    :is-link="!disabledinfo"
                    :value="sendvalue"
                    size="large"
                    @click="changeSend" />
          <template v-if="data.kaIssend==1">
            <van-field v-model="data.downCarrierSend"
                       type="number"
                       label="送货费"
                       size="large"
                       maxlength="6"
                       input-align="right"
                       placeholder="请输入"
                       :disabled="disabledinfo" />
          </template>
        </template>
        <van-action-sheet v-model="showPick"
                          :actions="actionsPick"
                          @select="onSelectPick"
                          cancel-text="取消"
                          @cancel="onCancelPick" />
        <van-action-sheet v-model="showSend"
                          :actions="actionsSend"
                          @select="onSelectSend"
                          cancel-text="取消"
                          @cancel="onCancelSend" />
      </van-cell-group>
    </div>
    <div class='message'>
      <van-field v-model="data.modifyRemark"
                 label="修改备注"
                 rows="1"
                 autosize
                 type="textarea"
                 show-word-limit
                 class='field'
                 maxlength="50"
                 required
                 :disabled="disabledinfo" />
    </div>
    <div class="carriageBtn">
      <div class="btn-wrap">
        <div class="btn-left"
             @click="handelCancel">
          <span>取消</span>
        </div>
        <div class="btn-right"
             @click="handelSure">
          <span>保存</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Vue from 'vue';
import { RadioGroup, Radio } from 'vant';

Vue.use(Radio);
Vue.use(RadioGroup);
import { mapGetters } from 'vuex';
import { DecryptToken } from '@/utils/auth';
import { orderSelect, orderChange } from '@/api/order';
import { kaCarrier } from '@/api/order';
export default {
  data () {
    return {
      showCarrier: false,
      showPick: false,
      showSend: false,
      departCost: '',
      payCost: '',
      receivableCost: '',
      time: '',
      Carrier: '请选择',
      message: '',
      carrierCost: '',
      pickCost: '',
      sendCost: '',
      pickvalue: '',
      sendvalue: '',
      carrierUser: '',
      carrierContact: '',
      carrierPhone: '',
      carrierNum: '',
      actionsCarrier: [
        { name: '精拼' },
        { name: '直客承运商' }
      ],
      actionsPick: [],
      actionsSend: [],
      iscarriage: false,
      isdown: '',
      data: {
        modifyRemark: ''
      }
    };
  },
  mounted () {
    this.orderId = this.$route.query.orderId;
    if (this.orderId) {
      this.getData();
    }
    this.getkaCarrier();
    this.changlocalStorage();
  },
  beforeRouteEnter (to, from, next) {
    let iscarriage = false;

    if (from.path === '/choiceCarriage') {
      iscarriage = true;
    }
    next((vm) => {
      vm.iscarriage = iscarriage;
     
    });
  },
  methods: {
    getkaCarrier () {
      kaCarrier({
        kaCarrierLikeName: '1'
      }).then(res => {
        console.log(res);
      });
    },
    changlocalStorage () {
      var carrierinfo = localStorage.getItem('carrierinfo');
      var carrierall = JSON.parse(localStorage.getItem('carrierall'));

      if (carrierinfo !== null) {
        this.Carrier = '直客承运商';
        this.carrierUser = carrierinfo;
      } else if (carrierall !== null) {
        this.Carrier = '直客承运商';
        this.carrierUser = carrierall.kaCarrierCompanyName;
        this.carrierContact = carrierall.kaCarrierContactName;
        this.carrierPhone = carrierall.kaCarrierContactMobile;
      }
    },
    getData () {
      orderSelect(this.orderId).then(res => {
        if (res.code == 200) {
      
          if (!this.iscarriage) {
            this.data = res.data;
            sessionStorage.setItem('bd-h5-selectcarrier', JSON.stringify(res.data));
           
          } else {
          
            let datas = sessionStorage.getItem('bd-h5-selectcarrier');

            this.data = datas ? JSON.parse(datas) : {};
           
          }
        }
        if (this.data.modifyRemark === null) {
          this.data.modifyRemark = '';
        }
     
        //判断是那种方式承运
        if (this.data.pushPlatformPc === null) {
          this.data.pushPlatformPc = 1;
        }
        this.data.pushPlatform = this.data.pushPlatformPc;
        let platform = '';

        this.isdown = this.data.carrierPrice;
        if (this.data.pushPlatform == 0) {
          platform = '--';
        } else if (this.data.pushPlatform == 1) {
          platform = '精拼';
        } else if (this.data.pushPlatform == 2) {
          platform = '德邦';
        } else {
          platform = '直客承运商';
        }
        this.isCarrier(platform);
   
        if (this.data.pushPlatform == 0) {
          this.carrierCost = '--';
          this.pickCost = '--';
          this.sendCost = '--';
          this.Carrier = '';
          this.sendvalue = '--';
          this.pickvalue = '--';
        } else {
          //判断是上游还是下游
          if (this.data.carrierPrice) {
            this.carrierCost = this.data.carrierPrice === null ? '--' : '¥' + this.data.carrierPrice;
            this.pickCost = this.data.carrierPickUp === null ? '短驳' : '¥' + this.data.carrierPickUp;
            this.sendCost = this.data.carrierSend === null ? '短驳' : '¥' + this.data.carrierSend;
            this.Carrier = platform;
            //判断是否提送货
            this.kaIspickUp();
          } else {
            this.Carrier = platform;
            this.carrierCost = this.data.downCarrierPrice === null ? '--' : '¥' + this.data.downCarrierPrice;
            this.pickCost = this.data.downCarrierPickUp === null ? '短驳' : '¥' + this.data.downCarrierPickUp;
            this.sendCost = this.data.downCarrierSend === null ? '短驳' : '¥' + this.data.downCarrierSend;
            this.kaIspickUp();
          }
        }
      });
    },
    isCarrier (val) {
      if (val == '精拼') {
        this.actionsPick = [
          { name: '精拼承运商' },
          { name: '短驳' }
        ];
        this.actionsSend = [
          { name: '精拼承运商' },
          { name: '短驳' }
        ];
        this.data.pushPlatform = 1;
        this.data.pushPlatformPc = 1;
      } else if (val == '直客承运商') {
        this.actionsPick = [
          { name: '直客承运商' },
          { name: '短驳' }
        ];
        this.actionsSend = [
          { name: '直客承运商' },
          { name: '短驳' }
        ];
        this.data.pushPlatform = 3;
        this.data.pushPlatformPc = 3;
      }
    },
    kaIspickUp () {
      var platform = '';

      if (this.data.pushPlatform == 0) {
        platform = '--';
      } else if (this.data.pushPlatform == 1) {
        platform = '精拼';
      } else if (this.data.pushPlatform == 2) {
        platform = '德邦';
      } else {
        platform = '直客承运商';
      }
      if (this.data.carrierPrice === null) {
        this.data.kaIspickUp = this.data.kaPickup;
        this.data.kaIssend = this.data.kaDelivery;
      }
      if (this.data.kaIspickUp == 0 || this.data.kaIspickUp === null) {
        this.pickvalue = '短驳';
        //this.data.carrierPickUp = null;
      } else if (this.data.kaIspickUp == 1) {
        this.pickvalue = platform == '直客承运商' ? platform : platform + '承运商';
      }
      if (this.data.kaIssend == 0 || this.data.kaIspickUp === null) {
        this.sendvalue = '短驳';
        //this.data.carrierSend = null;
      } else if (this.data.kaIssend == 1) {
        this.sendvalue = platform == '直客承运商' ? platform : platform + '承运商';
      }

    },
    onClickLeft () {
      sessionStorage.removeItem('bd-h5-selectcarrier');
      this.$router.back();
      // localStorage.removeItem('carrierinfo');
      // localStorage.removeItem('carrierall');
    },
    onClickRight () {
      let url = window.location.host;

      let index = url.indexOf('pre');

      if (index <= -1) {
        window.location.href = 'https://ka.lecarlink.com/index.aspx';
      } else {
        window.location.href = 'https://ka-pre.lecarlink.com/index.aspx';
      }
    },
    onSelectCarrier (item) {
      this.showCarrier = false;
      this.Carrier = item.name;
      this.data.kaIspickUp = 0;
      this.data.kaIssend = 0;
      this.pickvalue = '短驳';
      this.sendvalue = '短驳';
      this.data.carrierPickUp = null;
      this.data.carrierSend = null;
      this.data.carrierPrice = null;
      if (this.data.carrierPrice === null) {
        this.data.downCarrierPrice = null;
        this.data.downCarrierPickUp = null;
        this.data.downCarrierSend = null;
        // this.data.kaPickup = 0;
        // this.data.kaDelivery = 0;
      }
   
      this.isCarrier(item.name);
    },
    onCancelCarrier () {
      this.showCarrier = false;
    },
    changeCarrier () {
      if (this.disabledinfo) {
        return;
      }
      this.showCarrier = true;
    },
    onSelectPick (item) {
      this.showPick = false;
      this.pickvalue = item.name;
      if (item.name == '短驳') {
        this.data.kaIspickUp = 0;
        this.data.carrierPickUp = null;
        if (this.data.carrierPrice === null) {
          this.data.downCarrierPickUp = null;
        }
       
      } else {
        this.data.kaIspickUp = 1;
      }
    },
    onCancelPick () {
      this.showCarrier = false;
    },
    changePick () {
      if (this.disabledinfo) {
        return;
      }
      if (this.Carrier == '' || this.Carrier == '--') {
        this.$toast('请先选择干线承运商');
        return;
      }
      this.showPick = true;
    },
    changeSend () {
      if (this.disabledinfo) {
        return;
      }
      if (this.Carrier == '' || this.Carrier == '--') {
        this.$toast('请先选择干线承运商');
        return;
      }
      this.showSend = true;
    
    },
    changeContact () {
   
      this.showCarrierContact = true;
    },
    changeCarrierUser () {
      if (this.disabledinfo) {
        return;
      }
      if (this.data.pushPlatform == 3) {
        sessionStorage.setItem('bd-h5-selectcarrier', JSON.stringify(this.data));
        this.$router.push({
          path: '/choiceCarriage'
        });
      }
    },
    onSelectSend (item) {
      this.showSend = false;
      this.sendvalue = item.name;
      if (item.name == '短驳') {
        this.data.kaIssend = 0;
        this.data.carrierSend = null;
        if (this.data.carrierPrice === null) {
          this.data.downCarrierSend = null;
        }
      } else {
        this.data.kaIssend = 1;
      }
    },
    onCancelSend () {
      this.showSend = false;
    },
    handelCancel () {
 
      this.$router.back();
    },
    handelSure () {
      if (this.data.orderStatusBack >= 4) {
        this.$toast.fail('该订单状态不能修改承运信息');
        return;
      }
      if (this.data.modifyStatus == 1) {
        this.$toast.fail('该订单已在审核修改中');
        return;
      }
      this.data.kaOrderId = this.orderId;
      this.data.isAbnomal = 0;
      this.data.pushPlatformPc = this.data.pushPlatform;
      let user = DecryptToken.getUserInfo(this.getUserInfo.accessToken);

      this.data.kaInputUserId = user.userId;
      this.data.kaInputUserName = user.userName;

      if (this.data.carrierPrice === null) {
        this.data.carrierPrice = Number(this.data.downCarrierPrice);
        this.data.downCarrierPickUp === null ? this.data.carrierPickUp = null : this.data.carrierPickUp = Number(this.data.downCarrierPickUp);
        this.data.downCarrierSend === null ? this.data.carrierSend = null : this.data.carrierSend = Number(this.data.downCarrierSend);
        // this.data.carrierPickUp = Number(this.data.downCarrierPickUp);
        // this.data.carrierPrice = Number(this.data.downCarrierPrice);
        // this.data.carrierSend = Number(this.data.downCarrierSend);
      }
      if (this.downCarrierPickUp === null) {
        this.downCarrierPickUp = -1;
      }
      if (this.data.ganXianPrice === null) {
        this.data.ganXianPrice = this.data.kaArFreight;
        this.data.tiHuoPrice = this.data.kaArPickUp;
        this.data.faHuoPrice = this.data.kaArSend;
        this.data.carrierFlag = 0; // 确认承运的标识
      }
      this.data.peoplePrice = (Number(this.data.ganXianPrice) + Number(this.data.faHuoPrice) + Number(this.data.tiHuoPrice)).toFixed(2);
     
      if (this.validate()) {
        if(this.data.modifyRemark.trim()!="") {
          orderChange(this.data).then(res => {
       
          if (res.code == 200) {
            sessionStorage.removeItem('bd-h5-selectcarrier');
            this.$router.back();
          }
        });
        } else {
          this.$toast.fail("请填写修改订单备注");
           this.data.modifyRemark = "";
        }
        
        // alert('success');
      }
    },
    validate () {
      //获取电话号码
      var phone = this.data.kaCarrierContactMobile;
      var carrierPrice = this.data.carrierPrice;
      var carrierPickUp = this.data.carrierPickUp;
      var carrierSend = this.data.carrierSend;
      var kaIspickUp = this.data.kaIspickUp;
      var kaIssend = this.data.kaIssend;
      var kaCarrierCompanyName = this.data.kaCarrierCompanyName;
      var kaCarrierContactName = this.data.kaCarrierContactName;
      var carrierNumber = this.data.carrierNumber;
      var regPhone = /^1(3|4|5|6|7|8|9)\d{9}$/;

      var regNum = /^(-)?\d+(\.+\d{0,2})?$/; // 正负数两位小数

      let msg = '';


      if (!kaCarrierCompanyName && this.Carrier != '精拼') {
        msg += '请填写直客承运商\n';
        this.$toast.fail(msg);
        return false;
      } else if (!kaCarrierContactName && this.Carrier != '精拼') {
        msg += '请填写承运商联系人\n';
        this.$toast.fail(msg);
        return false;
      } else if (!phone && this.Carrier != '精拼') {
        msg += '请输入手机号\n';
        this.$toast.fail(msg);
        return false;
      } else if (!regPhone.test(phone) && this.Carrier != '精拼') {
        msg += '请输入正确的手机号\n';
        this.$toast.fail(msg);
        return false;
      } else if (!carrierNumber && this.Carrier != '精拼') {
        msg += '请填写承运商订单号\n';
        this.$toast.fail(msg);
        return false;
      } else if (carrierPrice === null) {
        msg += '请输入干线运输应付\n';
        this.$toast.fail(msg);
        return false;
      } else if (!regNum.test(carrierPrice)) {
        msg += '请输入正确干线运输应付\n';
        this.$toast.fail(msg);
        return false;
      } else if (!carrierPickUp && kaIspickUp == 1 && carrierPickUp != 0) {
       
        msg += '请输入提货费\n';
        this.$toast.fail(msg);
        return false;
      } else if (!regNum.test(carrierPickUp) && kaIspickUp == 1) {
        msg += '请输入正确的提货费\n';
        this.$toast.fail(msg);
        return false;
      } else if (!carrierSend && kaIssend == 1 && carrierSend != 0) {
        msg += '请输入送货费\n';
        this.$toast.fail(msg);
        return false;
      } else if (!regNum.test(carrierSend) && kaIssend == 1) {
         console.log(carrierSend,kaIssend,"1233333333");
        msg += '请输入正确的送货费\n';
        this.$toast.fail(msg);
        return false;
      } else if (!this.data.modifyRemark) {
        msg += '请填写修改订单备注\n';
        this.$toast.fail(msg);
        return false;
      }
      return true;
    }
  },
  computed: {
    ...mapGetters(['getUserInfo']),
    //0待提货 1-已提货 2-已承运 3-待承运 4-出运 7-到达站点 10派送中 13-签收(妥投) 14 异常签收  15 拒签 18回执已取
    disabledinfo () {
      return [4, 7, 10, 13, 14, 15, 18].includes(this.data.orderStatusBack);
    }
  }
};
</script>
<style lang="scss" scoped>
.carriage {
  width: 100%;
}

.carriageInfo {
  // width: 94%;
  padding-top: 56px;
  margin-left: 12px;
  margin-right: 12px;
  text-align: left;
  /deep/ .van-field__label {
    width: 3rem;
  }
}

.message {
  margin-top: 10px;
  margin-left: 12px;
  margin-right: 12px;
  padding-bottom: 60px;
}

.radio {
  font-size: 15px;
  height: 50px;
  background: #fff;
  padding-left: 20px;
}

.carriageBtn {
  width: 100%;
  background: #fff;
  height: 60px;
  position: fixed;
  bottom: 0;
  text-align: center;
  .btn-wrap {
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .btn-left {
      background: #fff;
      border: 1px solid #f18d00;
      border-radius: 2px;
      color: #f18d00;
      font-size: 18px;
      width: 47%;
      height: 44px;
      line-height: 44px;
      box-sizing: border-box;
    }
    .btn-right {
      width: 47%;
      height: 44px;
      line-height: 44px;
      font-size: 18px;
      background: #f18d00;
      border: 1px solid #f18d00;
      box-sizing: border-box;
      border-radius: 2px;
      color: #fff;
    }
  }
}
</style>

